package com.eos.orm.c3p0.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.eos.common.tool.CoreKeyContext;
import com.eos.orm.c3p0.dao.AbstractC3p0Dao;
import com.eos.orm.c3p0.dao.DBConnectionManager;
import com.eos.orm.mybatis.model.ViewBrokerageReport;
import com.hf.utils.StringUtil;

public class ViewBrokerageReportDaoImpl  extends AbstractC3p0Dao<ViewBrokerageReport>{

	public String condition1(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();
		if (null == map)
			return condition.append("1=0").toString(); 
		 
		if (!StringUtil.isEmpty(map.get("customer_id")))
			condition.append("customer_id=").append(map.get("customer_id"))
					.append(" and ");
		if (!StringUtil.isEmpty(map.get("start_time")))
			condition.append("settlement_account>='").append(map.get("start_time"))
					.append("' and ");
		
		if (!StringUtil.isEmpty(map.get("end_time")))
			condition.append("settlement_account<='").append(map.get("end_time"))
					.append("' and ");
		
		if (!StringUtil.isEmpty(map.get("status")))
			condition.append("status='").append(map.get("status"))
					.append("' and ");
		
		condition.append("1=1");
		if(!StringUtil.isEmpty(map.get("order_by"))){
			condition.append(map.get("order_by"));
		}
		return condition.toString();
	}
	
	public List<ViewBrokerageReport> findPage(Map<String, String> map){
		List<ViewBrokerageReport> templist = new ArrayList<ViewBrokerageReport>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = StringUtil.replace(StringUtil.replace(StringUtil.replace(
				CoreKeyContext.getCoreKey("tabs_page_sqlscript"),
				"$table_name$", map.get("table_name")), "$condition1$",
				condition1(map)), "$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ViewBrokerageReport temp = new ViewBrokerageReport();
				temp.setCustomerId(rs.getInt("customer_id"));
				temp.setCustomerTotalMoney(rs.getDouble("customer_total_money"));
				temp.setInsertTime(rs.getString("insert_time"));
				temp.setModifyTime(rs.getString("modify_time"));
				temp.setSettlementAccount(rs.getString("settlement_account"));
				temp.setStatus(rs.getString("status"));
				templist.add(temp);
				temp = null;
			}
		} catch (SQLException e) {
			System.out
					.println("product_model exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
}
